// pages/xiugai/xiugai.js
var app = getApp()
let api = require('../../../api/api.js')
var util = require("../../../utils/util.js")
var codeTimer = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navbar:[{
      name:"信息编辑"
    }, {
        name: "修改电话"
      }],
    phone: "",
    codeHidden: true,
    timerNumber: 60,
    currentTab:0,
    currentTabsIndex:0
  },

  navbar:function(e){
    var index = e.currentTarget.dataset.index
    this.setData({
      currentTabsIndex : index
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (data) {
    var that = this
    api.ditch(data, res => {
      that.setData({
        info: res.data.result
      })
    })
    that.setData({
      options: data
    })
  },
  formSubmit:function(e){
    var param = e.detail.value
    param.id = this.data.options.id
    console.log(param)
    util.sendRequest("/api/dblong/updatelist",param,"POST",false,function(res){
      console.log(res)
      if(res.result == 1){
        wx.navigateBack({
          delta:1
        })
      }
    })
  },

  phone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  code: function (e) {
    this.setData({
      code: e.detail.value
    })
  },

  getCode: function (e) {
    console.log("获取验证码")
    var that = this
    var phone = this.data.phone
    console.log(phone)
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
      util.showError("手机号码有误，请重填！")
      console.log("手机号码有误，请重填！")
    } else {
      //修改店铺传type=2
      var data = {}
      data.newmobile = phone
      // data.type = 2
      api.updatecode(data, res => {
        console.log(res)
        if (res.data.code != "成功") {
          util.showError(res.code)
        } else {
          that.setData({
            codeHidden: !that.data.codeHidden,
            timerNumber: 60
          });
          codeTimer = setInterval(that.codeTimerFn, 1000);
        }
      })
    }
  },
  codeTimerFn: function () {
    var that = this;
    that.setData({
      timerNumber: that.data.timerNumber - 1
    });

    if (that.data.timerNumber == 0) {
      clearInterval(codeTimer);
      codeTimer = null;

      that.setData({
        codeHidden: !that.data.codeHidden
      });
    }
  },

  login:function(){
    var data = {}
    data.mobile = this.data.options.mobile
    data.newmobile = this.data.phone
    data.verify = this.data.code
    data.id = this.data.options.id
    console.log(data)
    api.updateditch(data, res => {
      console.log(res)
      if (res.data.code == '成功') {
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

